package com.haha.myapplication;

import android.provider.ContactsContract;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.haha.myapplication.bean.Note;
import com.haha.myapplication.util.ToastUtil;
import com.haha.myapplication.NoteDbOpenHelper;
import com.haha.myapplication.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AddActivity extends AppCompatActivity {

    private EditText etTitle,etContent;

    private NoteDbOpenHelper mNoteDbOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);
        mNoteDbOpenHelper = new NoteDbOpenHelper(this);

    }

    public void add(View view) {
        //点击add获取到标题和内容
        String title = etTitle.getText().toString();
        String content = etContent.getText().toString();
        if (TextUtils.isEmpty(title)){
            ToastUtil.toastShort(this,"标题不能为空");
            return;
        }

        //以实体形式保存到数据库中
        Note note = new Note();
        note.setTitle(title);
        note.setContent(content);
        note.setCreatedTime(getCurrentTimeFormat());
        long row = mNoteDbOpenHelper.insertData(note);
        if (row != -1){
            ToastUtil.toastShort(this,"添加成功");
            //添加成功后需要把列表自动返回到列表界面
            this.finish();
        }else {
            ToastUtil.toastShort(this,"添加失败");
        }
    }

    //获取手机当前时间，并格式化
    private String getCurrentTimeFormat(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }
}